package com.thientan.controller.core

import org.springframework.context.MessageSource

import com.thientan.model.NhanVien

import grails.converters.JSON

class UserController {
	MessageSource messageSource
	
    def login() {
		if(session.isLogged){
			redirect (uri : '/')
		}
	}
	
	def doLogin(){
		def user = NhanVien.withCriteria(uniqueResult : true) {
			eq('maNv', params.username)
			eq('password', params.password.encodeAsPassword())
			eq('kichHoat', true)
		}
		if(user){
			session.isLogged = true
			session.user = user
			redirect (uri : '/')
		} else {
			flash.message = messageSource.getMessage('default.thientan.messages.invalidUser', null, null)
			redirect(action : 'login')
		}
	}
	
	def logout() {
		session.isLogged = false
		session.user = null
		redirect (uri : '/')
	}
	
	def loadLoggedUserInfo() {
		Map result = ["success" : true]
		result.putAt("user", session.user?.hoTen ?: '')
		render result as JSON
	}
}
